package com.example.demo.Utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.example.demo.exception.GlobalException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;


public class CreateSimpleExcelToDisk {

    /**
     * 手工创建一个包含student的list
     * @return
     * @throws Exception
     */
/*    private static List<Student> getStudent() throws Exception {
        List<Student> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        Student user1 = new Student(1, "张三", 16, sdf.parse("1997-03-12"));
        Student user2 = new Student(2, "李四", 17, sdf.parse("1996-08-12"));
        Student user3 = new Student(3, "王五", 26, sdf.parse("1985-11-12"));
        list.add(user1);
        list.add(user2);
        list.add(user3);

        return list;
    }*/


    /**
     * 创建文件
     * <p>
     * <p>
     * [{"kname":1,"vname":"YQJ","ranking":"22","addtime":24}]
     * ["\u699c\u540d","\u54c1\u724c","\u7968\u6570","\u54c1\u724c\u521b\u5efa\u65f6\u95f4"]  头
     *
     * @param list
     */
    public String creatFile(String[] header, List dataList) throws IOException {
        long time = TimeUtil.getTime();
        String dateStr = TimeUtil.stampToDate(time, "yyyy-MM-dd");


        if (header.length == 0) throw new GlobalException("header 数据错误 ");
        if (dataList.size() == 0) throw new GlobalException("dataList 数据错误 ");
        //第一步,创建一个webbook文件,对应一个excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //第二部,在excel中添加一个sheet工作簿,参数为该工作簿名字,不写为默认;
        HSSFSheet sheet = wb.createSheet("文件1");
        //第三部,做sheet中添加表头第0行,注意老版本poi对excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        //第四部,创建单元格表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//创建一个居中格式

        //创建具体盛放数据的单元格,可以考虑把cell抽成共通对象去使用
        HSSFCell cell = null;
        for (int i = 0; i < header.length; i++) {
            cell = row.createCell((int) i);
            cell.setCellValue(header[i]);
        }

        for (int i = 0; i < dataList.size(); i++) {
            //每次新建一行然后在新行中插入list中的数据对象,有点繁琐,也许有更好的封装方法,留待后看
            row = sheet.createRow((int) i + 1);
            Map<String, String> map = (Map<String, String>) dataList.get(i);

            int a = 0;
            for (String key : map.keySet()) {
                String value = map.get(key);
                System.out.println(key + "  " + value);
                row.createCell((int) a).setCellValue(value);
                a++;
            }
        }
        String str_Ran = RandomUtilTest.generateStringNum(11);
        String fileName = str_Ran + ".xlsx";
        File directory = new File("");// 参数为空
        String courseFile = directory.getCanonicalPath();
        String filePath = courseFile + File.separator + "uploads" + File.separator + dateStr + File.separator;
        QRCodeUtils.mkdirs(filePath);
        String fileFullPath = filePath + File.separator + fileName;

        //第六部,将文件保存到指定位置
        FileOutputStream fout = new FileOutputStream(fileFullPath);
        wb.write(fout);
        fout.close();

        String Newfile = "/uploads/" + dateStr + "/" + fileName;
        return Newfile;
    }


    public static void main(String[] args) throws Exception {



    }
}
